package com.github.phoenics.militia.bazookaFx.generator.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.github.phoenics.militia.bazookaFx.domain.entity.AppLink;
import com.github.phoenics.militia.bazookaFx.generator.config.converts.*;
import com.github.phoenics.militia.bazookaFx.generator.config.querys.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * @author Phoenics Chow
 * on 2019年02月12日
 */
public class QueryTemplate {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(QueryTemplate.class);
    private AppLink appLink;
    private JdbcTemplate jdbcTemplate;

    /**
     * 数据库信息查询
     */
    private  IDbQuery dbQuery;
    /**
     * 数据库类型
     */
    private DbType dbType;
    /**
     * 类型转换
     */
    private  ITypeConvert typeConvert;

    public QueryTemplate(AppLink appLink){
        this.appLink=appLink;
        init();
    }
    private void init() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(this.appLink.getDriverClassName());
        dataSource.setUrl(this.appLink.getUrl());
        dataSource.setUsername(this.appLink.getUsername());
        dataSource.setPassword(this.appLink.getPassword());
        jdbcTemplate = new JdbcTemplate(dataSource);
    }
    public boolean testLink(){
        int i=jdbcTemplate.queryForObject(getDbQuery().testSql(),Integer.class);
        return i==1;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public AppLink getAppLink(){
        return appLink;
    }
    public IDbQuery getDbQuery() {
        if (null == dbQuery) {
            switch (getDbType()) {
                case ORACLE:
                    dbQuery = new OracleQuery();
                    break;
                case SQL_SERVER:
                    dbQuery = new SqlServerQuery();
                    break;
                case POSTGRE_SQL:
                    dbQuery = new PostgreSqlQuery();
                    break;
                case DB2:
                    dbQuery = new DB2Query();
                    break;
                case MARIADB:
                    dbQuery = new MariadbQuery();
                    break;
                case H2:
                    dbQuery = new H2Query();
                    break;
                default:
                    // 默认 MYSQL
                    dbQuery = new MySqlQuery();
                    break;
            }
        }
        return dbQuery;
    }
    public DbType getDbType() {
        String str=appLink.getDbType().toLowerCase();
        if (str.contains("mysql")) {
            return DbType.MYSQL;
        } else if (str.contains("oracle")) {
            return DbType.ORACLE;
        } else if (str.contains("postgresql")) {
            return DbType.POSTGRE_SQL;
        } else if (str.contains("sqlserver")) {
            return DbType.SQL_SERVER;
        } else if (str.contains("db2")) {
            return DbType.DB2;
        } else if (str.contains("mariadb")) {
            return DbType.MARIADB;
        } else if (str.contains("h2")) {
            return DbType.H2;
        } else {
            return null;
        }
    }
    public ITypeConvert getTypeConvert() {
        if (null == typeConvert) {
            switch (getDbType()) {
                case ORACLE:
                    typeConvert = new OracleTypeConvert();
                    break;
                case SQL_SERVER:
                    typeConvert = new SqlServerTypeConvert();
                    break;
                case POSTGRE_SQL:
                    typeConvert = new PostgreSqlTypeConvert();
                    break;
                case DB2:
                    typeConvert = new DB2TypeConvert();
                    break;
                case MARIADB:
                    typeConvert = new MySqlTypeConvert();
                    break;
                default:
                    // 默认 MYSQL
                    typeConvert = new MySqlTypeConvert();
                    break;
            }
        }
        return typeConvert;
    }
}
